//
//  HomeBottomView.m
//  TaurusXi
//
//  Created by xicheng on 15/11/27.
//  Copyright © 2015年 xicheng. All rights reserved.
//

#import "HomeBottomView.h"

static const NSInteger tagIndex = 1221;

@interface HomeBottomView()

@property (nonatomic,strong) UIView *lineView;

@property (nonatomic,strong) UIButton *homeBt;

@property (nonatomic,strong) UIButton *myBt;


@end

@implementation HomeBottomView


- (UIView *) lineView{
  if (_lineView == nil) {
    
    UIImageView *lineView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [DisplayUtils deviceWidth], 1)];
    lineView.image = [UIImage imageNamed:@"ic_space_line"];
    _lineView = lineView;
  }
  return _lineView;
}


- (id) initWithFrame:(CGRect)frame{
  self = [super initWithFrame:frame];
  if (self) {
    [self createBottomView:frame];
    self.backgroundColor = RGB(8, 32, 71);
  }
  return self;
}

-(void) createBottomView:(CGRect)frame{
//  NSArray *titleArray = @[@"首页",@"我的"];
  
  CGFloat halfWidth =  [DisplayUtils deviceBounds].size.width/2;
  for (int i=0; i<2; i++) {
    
    UIButton *button = [self  buttonWithFrame:CGRectMake(halfWidth*i, 0,halfWidth, frame.size.height) title:nil tag:tagIndex+i];
    [button addTarget:self action:@selector(bottomBarAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    if (i==0) {
      self.homeBt = button;
    }else{
      self.myBt = button;
    }
  
  }
  [self addSubview:self.lineView];
  [self.homeBt setSelected:YES];
}


- (UIButton *) buttonWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag{
  
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  button.frame = frame;
  if (tag==tagIndex) {
    UIImage *normalImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ic_space_home_click@2x" ofType:@"png" ]];
    [button setImage:normalImage forState:UIControlStateSelected ];
    [button setImage:normalImage forState:UIControlStateHighlighted ];
    [button setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ic_space_home@2x" ofType:@"png"]] forState:UIControlStateNormal ];
  }else if (tag ==tagIndex+1){
    UIImage *normalImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ic_space_space_click@2x" ofType:@"png" ]];
    [button setImage:normalImage forState:UIControlStateSelected ];
    [button setImage:normalImage forState:UIControlStateHighlighted ];
    [button setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ic_space_space@2x" ofType:@"png" ]] forState:UIControlStateNormal ];
  }
//  [button setTitle:title forState:UIControlStateNormal];
//  [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//  button.titleLabel.font = [UIFont systemFontOfSize:16];
  button.tag = tag;
  return button;
}

- (void) bottomBarAction:(UIButton *)sender{
  
  if (sender.selected) {
    return;
  }
  
  Boolean selectedFlag = YES;
  
  if (sender.tag == tagIndex) {
    [self.homeBottomChangeDelegate selectMainAction];
   
  }else if (sender.tag == tagIndex+1){
    selectedFlag = [self.homeBottomChangeDelegate selectMyAction];
  }
  
  if (!selectedFlag) {
    return;
  }
  
  Boolean isHomeSelected = sender == self.homeBt;
  
  self.homeBt.selected = isHomeSelected?YES:NO;
  self.myBt.selected = isHomeSelected?NO:YES;

  
}

@end
